package com.mes.cloud.base.feign;

import feign.Contract;
import feign.Feign;
import feign.InvocationHandlerFactory;
import feign.hystrix.HystrixDelegatingContract;
import org.springframework.beans.factory.BeanFactory;
/**
 * @see feign.hystrix.HystrixFeign.Builder
 * Created by Lenovo on 2017/7/25.
 */
public class FeignBuilder extends Feign.Builder {

    private BeanFactory beanFactory;

    private boolean inTest;

    public FeignBuilder(BeanFactory beanFactory, boolean inTest) {
        this.beanFactory = beanFactory;
        this.inTest = inTest;
    }

    private Contract contract = new Contract.Default();

    @Override
    public Feign.Builder invocationHandlerFactory(InvocationHandlerFactory invocationHandlerFactory) {
        throw new UnsupportedOperationException();
    }

    @Override
    public FeignBuilder contract(Contract contract) {
        this.contract = contract;
        return this;
    }

    @Override
    public Feign build() {
        super.invocationHandlerFactory((target, dispatch) ->
                new FeignInvocationHandler(beanFactory, inTest, target, dispatch));
        super.contract(new HystrixDelegatingContract(contract));
        return super.build();
    }
}

